{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!
Generated Python enums for Thrift types
}}
{{> common/auto_generated_py}}
import builtins

import apache.thrift.metadata.thrift_types as _fbthrift_metadata
import {{program:base_library_package}}.types as _fbthrift_python_types
{{#program:enums}}
import typing as _std_python_typing

{{!
    This is necessary because thrift_abstract_types.py imports this.
}}


class {{enum:name}}(_fbthrift_python_types.{{!
        }}{{#enum:flags?}}Flag{{/enum:flags?}}{{!
        }}{{^enum:flags?}}Enum, int{{/enum:flags?}}{{!
        }}):
    {{#enum:values}}
    {{enum_value:py_name}} = {{enum_value:value}}
    {{/enum:values}}
    @staticmethod
    def __get_thrift_name__() -> builtins.str:
        return "{{program:name}}.{{enum:name}}"

    @staticmethod
    def __get_thrift_uri__() -> _std_python_typing.Optional[builtins.str]:
        return {{#enum:uri}}"{{enum:uri}}"{{/enum:uri}}{{!
            }}{{^enum:uri}}None{{/enum:uri}}

    @staticmethod
    def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
        return gen_metadata_enum_{{enum:name}}()

    def _to_python(self) -> "{{enum:name}}":
        return self

    def _to_py3(self) -> "{{enum:name}}":
    {{! NB: thrift-py3 now uses thrift-python enums}}
        return self

    def _to_py_deprecated(self) -> int:
        return self.value
{{/program:enums}}

{{#program:enums}}
def _fbthrift_gen_metadata_enum_{{enum:name}}({{!
}}metadata_struct: _fbthrift_metadata.ThriftMetadata) -> _fbthrift_metadata.ThriftMetadata:
    qualified_name = "{{program:name}}.{{enum:name}}"

    if qualified_name in metadata_struct.enums:
        return metadata_struct
    elements = {
        {{#enum:values}}
        {{enum_value:value}}: "{{enum_value:name}}",
        {{/enum:values}}
    }
    structured_annotations = [
        {{#enum:structured_annotations}}
        {{#structured_annotation:const_struct?}}{{#structured_annotation:const}}{{#constant:value}}
        {{> metadata/const_struct }},
        {{/constant:value}}{{/structured_annotation:const}}{{/structured_annotation:const_struct?}}
        {{/enum:structured_annotations}}
    ]
    enum_dict = dict(metadata_struct.enums)
    enum_dict[qualified_name] = _fbthrift_metadata.ThriftEnum(name=qualified_name, elements=elements, structured_annotations=structured_annotations)
    new_struct = metadata_struct(enums=enum_dict)

    return new_struct

def gen_metadata_enum_{{enum:name}}() -> _fbthrift_metadata.ThriftMetadata:
    return _fbthrift_gen_metadata_enum_{{enum:name}}(
        _fbthrift_metadata.ThriftMetadata(structs={}, enums={}, exceptions={}, services={})
    )

{{/program:enums}}
